//
//  TrainShiftVC.m
//  ChangJiuGroup
//
//  Created by Hank on 2017/11/17.
//  Copyright © 2017年 周昊. All rights reserved.
//
#define TransportTitle @"请选择运输方式"
#define LocationTitle  @"请选择位置"
#define OriginTitle    @"请选择起始地"
#define TerminalTitle  @"请选择目的地"
#define SupplierTitle  @"请选择供应商"

#import "TrainShiftVC.h"
#import "UINavigationBar+Awesome.h"
#import "BinningCarVC.h"
#import "DrCorner.h"
#import "STPickerDate.h"
#import "STPickerSingle.h"
#import "TrainShiftModel.h"
#import "UIButton+Expand.h"
#import "BinningListVC.h"
#import "SaveSortVC.h"
#import "UIButton+Expand.h"
#import "UnstuffingVC.h"
#import "UnloadingAndHangBoxVC.h"
#import "QueryDimVC.h"
//#import "YHPullDownMenu.h"

@interface TrainShiftVC ()<STPickerDateDelegate,STPickerSingleDelegate>
// 火车方向数组
@property (strong, nonatomic)TrainDictItemModel *trainDictItemModel;
// 方向按钮数组
@property (strong, nonatomic)NSMutableArray *directionBtnArray;
// 选择的方向按钮
@property (strong, nonatomic)UIButton *selectDirectionBtn;

@end

@implementation TrainShiftVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置导航栏
    [self setNavC];
    // 设置界面属性
    [self setViewAttribute];
    // 设置BarButtonItem
    [self setBarButtonItem];
    // 请求数据
    [self requestData];
    // 设置圆角
    [self setRadius];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

// 请求数据
- (void)requestData
{
//    NSDictionary *info = @{@"item":@"1019"};
//    // 防止循环引用
//    WEAK_REF(self);
//    [[CSSManager getPackingOperator] postDictItem:info tag:self successBlock:^(id respondObject) {
//        // 测试列车方向接口
////        NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"TrainShiftVC" ofType:@"json"]];
////        respondObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//        //获取数据
//        weak_self.trainDictItemModel = [[TrainDictItemModel alloc] initWithDictionary:Infos error:nil];
//        // 加载方向按钮
//        [weak_self loadDirectionBtn:weak_self.trainDictItemModel.retmarkobj];
//    } failureBlock:^(id respondObject) {
//        // 解析返回错误信息
//        ReturnErrorModel *returnErrorModel = [[ReturnErrorModel alloc] initWithDictionary:Infos error:nil];
//        // 显示错误信息
//        [MBProgressHUD showError:returnErrorModel.retmsg];
//    }];
    
    // 测试列车方向接口
    NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"TrainShiftVC" ofType:@"json"]];
    id respondObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    //获取数据
    self.trainDictItemModel = [[TrainDictItemModel alloc] initWithDictionary:Infos error:nil];
    // 加载方向按钮
    [self loadDirectionBtn:self.trainDictItemModel.retmarkobj];
}

// 加载方向按钮
- (void)loadDirectionBtn:(NSArray *)dataArray;
{
    // 横向间隙, 竖向间隙, 格子的宽, 格子的高
    int horizontalMargin = 70, verticalMargin = 15, width = 100, height = 30;
    // 更新方向背景视图约束的高度
    self.directionViewH.constant = (dataArray.count % 2 + dataArray.count / 2) * (height + verticalMargin) - verticalMargin;
    for (int i = 0; i < dataArray.count; i++) {
        // 获取列车方向 Model
        TrainRetmarkobjModel *trainRetmarkobjModel = dataArray[i];
        // 计算列行
        int row = i/2, col = i%2;
        // 加载方向按钮
        UIButton *directionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        directionBtn.frame = CGRectMake(col * (width + horizontalMargin),row * (height + verticalMargin), width, height);
        [directionBtn setTitle:trainRetmarkobjModel.dictValue forState:UIControlStateNormal];
        [directionBtn setTitleColor:DefaultGray forState:UIControlStateNormal];
        // 设置圆角
        [directionBtn radiusBtn:3 backgroundColor:[UIColor whiteColor] borderWidth:0.5 borderColor:DefaultRadiusGray];
        // 默认第一个选择
        if (i == 0) {
            // 选择方向按钮点击
            [self directionSelectBtn:directionBtn];
        }
        directionBtn.tag = [trainRetmarkobjModel.dictKey intValue];
        directionBtn.titleLabel.font = Text_Font(15);
        [directionBtn addTarget:self action:@selector(travelDirectionAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.directionBgView addSubview:directionBtn];
        [self.directionBtnArray addObject:directionBtn];
    }
}

#pragma mark - initArray
// 初始化数组
- (NSMutableArray *)directionBtnArray
{
    if (!_directionBtnArray) {
        _directionBtnArray = [[NSMutableArray alloc]init];
    }
    return _directionBtnArray;
}

#pragma mark - 导航栏
- (void)setNavC
{
    self.title = @"首页";
    // 设置导航栏颜色
    BasicNavigationController *nVC = (BasicNavigationController *)self.navigationController;
    nVC.themeColor = [UIColor whiteColor];
    nVC.tintColor = [UIColor darkGrayColor];
}

// 设置界面属性
- (void)setViewAttribute
{
    self.view.backgroundColor = [UIColor whiteColor];
    // 修改选择框字体大小
    [self.selectBox setTitleTextAttributes:@{NSFontAttributeName:Text_Font(15)}forState:UIControlStateNormal];
    // 初始化约束(装箱, 掏箱, 卸箱)
    [self initConstraint];
}

// 设置BarButtonItem
- (void)setBarButtonItem
{
    UIButton *leftItemBtn = [UIButton setLeftOrRightButton:@"login_userName" direction:@"left" title:nil];
    [leftItemBtn addTarget:self action:@selector(leftItemAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftItemBtn];
    
    UIButton *rightItemBtn = [UIButton setLeftOrRightButton:@"login_userName" direction:@"right" title:nil];
    [rightItemBtn addTarget:self action:@selector(rightItemAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightItemBtn];
}

#pragma mark - itemAction
- (void)leftItemAction
{
    // 跳转到本地保存分类装车数据列表界面
    SaveSortVC *saveSortVC = [[SaveSortVC alloc]init];
    [self.navigationController pushViewController:saveSortVC animated:YES];
}

- (void)rightItemAction
{
    QueryDimVC *queryDimVC = [[QueryDimVC alloc]init];
    [self.navigationController pushViewController:queryDimVC animated:YES];
}

#pragma mark - Picker
// 显示日期选择器
- (void)showDatePicker
{
    STPickerDate *datePicker = [[STPickerDate alloc]init];
    [datePicker setDelegate:self];
    [datePicker setYearLeast:2000];
    [datePicker setYearSum:50];
    [datePicker setContentMode:STPickerContentModeBottom];
    [datePicker show];
}
// 显示内容选择器
- (void)showContentPicker:(NSMutableArray *)dataArray pickerTitle:(NSString *)pickerTitle
{
    // 创建内容选择器
    STPickerSingle *pickerSingle = [[STPickerSingle alloc]init];
    // 添加数据
    [pickerSingle setArrayData:dataArray];
    // 添加标题
    [pickerSingle setTitle:pickerTitle];
    [pickerSingle setContentMode:STPickerContentModeBottom];
    [pickerSingle setDelegate:self];
    // 显示内容选择器
    [pickerSingle show];
}

#pragma mark - Radius
// 设置圆角
- (void)setRadius
{
    [self.transportBtn radiusBtn:3 backgroundColor:[UIColor clearColor] borderWidth:0.5 borderColor:DefaultRadiusGray];
    [self.dateBtn radiusBtn:3 backgroundColor:[UIColor clearColor] borderWidth:0.5 borderColor:DefaultRadiusGray];
    [self.locationBtn radiusBtn:3 backgroundColor:[UIColor clearColor] borderWidth:0.5 borderColor:DefaultRadiusGray];
    [self.originBtn radiusBtn:3 backgroundColor:[UIColor clearColor] borderWidth:0.5 borderColor:DefaultRadiusGray];
    [self.terminalBtn radiusBtn:3 backgroundColor:[UIColor clearColor] borderWidth:0.5 borderColor:DefaultRadiusGray];
    [self.supplierBtn radiusBtn:3 backgroundColor:[UIColor clearColor] borderWidth:0.5 borderColor:DefaultRadiusGray];
    [self.nextStepBtn radiusBtn:3 backgroundColor:DefaultGray borderWidth:0 borderColor:[UIColor clearColor]];
}

#pragma mark - Extract 提取
// 选择方向按钮点击
- (void)directionSelectBtn:(UIButton *)selectBtn
{
    // 选择按钮
    selectBtn.selected = YES;
    // 设置选中字体颜色
    [selectBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    selectBtn.layer.backgroundColor = DefaultGray.CGColor;
    // 存储选择方向的按钮
    self.selectDirectionBtn = selectBtn;
}
// 初始化约束(装箱, 掏箱, 卸箱)
- (void)initConstraint
{
    // 显示位置
    self.locationBtnH.constant = 30;
    self.locationBtnY.constant = 15;
    self.locationBtn.hidden = NO;
    // 隐藏运输 起始点 目的地方式
    self.transportH.constant = self.transportY.constant = self.originBtnH.constant = self.originBtnY.constant = self.terminalBtnH.constant = self.terminalBtnY.constant = 0;
    self.transportBtn.hidden = self.originBtn.hidden = self.terminalBtn.hidden = YES;
}

#pragma mark - Action
// 列车行驶方向
- (IBAction)travelDirectionAction:(UIButton *)sender
{
    // 还原选中状态
    for (int i = 0 ; i < self.directionBtnArray.count; i++) {
        UIButton *directionBtn = self.directionBtnArray[i];
        directionBtn.selected = NO;
        [directionBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        // 设置圆角
        directionBtn.layer.backgroundColor = [UIColor whiteColor].CGColor;
    }
    // 选择方向按钮点击
    [self directionSelectBtn:sender];
}
// 运输方式 Action
- (IBAction)transportAction:(UIButton *)sender
{
    // 显示内容选择器
    [self showContentPicker:[NSMutableArray arrayWithArray:@[@"运输方式测试1",@"运输方式测试2",@"运输方式测试3",@"运输方式测试4",@"运输方式测试5",@"运输方式测试6"]] pickerTitle:TransportTitle];
}
// 日期 Action
- (IBAction)dateAction:(UIButton *)sender
{
    // 显示日期选择器
    [self showDatePicker];
}
// 位置 Action
- (IBAction)locationAction:(UIButton *)sender
{
    // 显示内容选择器
    [self showContentPicker:[NSMutableArray arrayWithArray:@[@"位置测试1",@"位置测试2",@"位置测试3",@"位置测试4",@"位置测试5",@"位置测试6"]] pickerTitle:LocationTitle];
}
// 起始地 Action
- (IBAction)originAction:(UIButton *)sender
{
    // 显示内容选择器
    [self showContentPicker:[NSMutableArray arrayWithArray:@[@"起始地测试1",@"起始地测试2",@"起始地测试3",@"起始地测试4",@"起始地测试5",@"起始地测试6"]] pickerTitle:OriginTitle];
}
// 目的地 Action
- (IBAction)terminalAction:(UIButton *)sender
{
    // 显示内容选择器
    [self showContentPicker:[NSMutableArray arrayWithArray:@[@"目的地测试1",@"目的地测试2",@"目的地测试3",@"目的地测试4",@"目的地测试5",@"目的地测试6"]] pickerTitle:TerminalTitle];
}
// 供应商 Action
- (IBAction)supplierAction:(UIButton *)sender
{
    // 显示内容选择器
    [self showContentPicker:[NSMutableArray arrayWithArray:@[@"供应商测试1",@"供应商测试2",@"供应商测试3",@"供应商测试4",@"供应商测试5",@"供应商测试6"]] pickerTitle:SupplierTitle];
}
// 下一步 Action
- (IBAction)nextStepBtnAction:(UIButton *)sender
{
    if (!self.selectDirectionBtn) {
        [MBProgressHUD showError:@"请选择列车行驶方向"];
        return;
    }
    if (self.selectedSegmentIndex == 3) {
        if(JudgeStrEqualNull(_transportBtn.titleLabel.text)) {
            [MBProgressHUD showError:@"请选择运输方式"];
            return;
        }
    }
    if(JudgeStrEqualNull(_dateBtn.titleLabel.text)) {
        [MBProgressHUD showError:@"请选择日期"];
        return;
    }
    if (self.selectedSegmentIndex == 3) {
        if(JudgeStrEqualNull(_originBtn.titleLabel.text)) {
            [MBProgressHUD showError:@"请选择起始地"];
            return;
        }
    }
    if (self.selectedSegmentIndex == 3) {
        if(JudgeStrEqualNull(_terminalBtn.titleLabel.text)) {
            [MBProgressHUD showError:@"请选择目的地"];
            return;
        }
    }
    if (self.selectedSegmentIndex < 3) {
        if(JudgeStrEqualNull(_locationBtn.titleLabel.text)) {
            [MBProgressHUD showError:@"请选择位置"];
            return;
        }
    }
    if(JudgeStrEqualNull(_supplierBtn.titleLabel.text)) {
        [MBProgressHUD showError:@"请选择供应商"];
        return;
    }
    // 创建TrainShiftModel
    TrainShiftModel *trainShiftModel = [[TrainShiftModel alloc]init];
    trainShiftModel.departDirection = [NSString stringWithFormat:@"%ld",self.selectDirectionBtn.tag];
    // 暂时暂定西向为1  东向为2
    trainShiftModel.departDirectionType = [self.selectDirectionBtn.titleLabel.text isEqualToString:@"西向"] ? @"1" : @"2";
    trainShiftModel.date = _dateBtn.titleLabel.text;
    trainShiftModel.location = _locationBtn.titleLabel.text;
    trainShiftModel.supplier = _supplierBtn.titleLabel.text;
    
    switch (self.selectedSegmentIndex) {
        case 0:{
            // 跳转装箱列表(装车, 装支架, 装散货, 装零部件)
            BinningListVC *binningListVC = [[BinningListVC alloc]init];
            binningListVC.trainShiftModel = trainShiftModel;
            [self.navigationController pushViewController:binningListVC animated:YES];
            break;
        }
        case 1:{
            // 跳转掏箱
            UnstuffingVC *unstuffingVC = [[UnstuffingVC alloc]init];
            unstuffingVC.trainShiftModel = trainShiftModel;
            [self.navigationController pushViewController:unstuffingVC animated:YES];
            break;
        }
        case 2:{
            // 跳转卸箱
            UnloadingAndHangBoxVC *unloadingAndHangBoxVC = [[UnloadingAndHangBoxVC alloc]init];
            unloadingAndHangBoxVC.trainShiftModel = trainShiftModel;
            unloadingAndHangBoxVC.typeJudge = @"Unloading";
            [self.navigationController pushViewController:unloadingAndHangBoxVC animated:YES];
            break;
        }
        case 3:{
            // 跳转吊箱
            UnloadingAndHangBoxVC *unloadingAndHangBoxVC = [[UnloadingAndHangBoxVC alloc]init];
            unloadingAndHangBoxVC.trainShiftModel = trainShiftModel;
            unloadingAndHangBoxVC.typeJudge = @"Hang";
            [self.navigationController pushViewController:unloadingAndHangBoxVC animated:YES];
            break;
        }
        default:
            break;
    }
}

// 选择操作类型(装箱, 掏箱, 卸箱, 吊箱) Action
- (IBAction)selectType:(UISegmentedControl *)sender
{
    // 保存选中操作类型(装箱, 掏箱, 卸箱, 吊箱)
    self.selectedSegmentIndex = sender.selectedSegmentIndex;
    
    if (sender.selectedSegmentIndex == 3) {
        // 显示运输 起始点 目的地方式
        self.transportH.constant = self.originBtnH.constant = self.terminalBtnH.constant = 30;
        self.transportY.constant = self.originBtnY.constant = self.terminalBtnY.constant = 15;
        self.transportBtn.hidden = self.originBtn.hidden = self.terminalBtn.hidden = NO;
        // 隐藏位置
        self.locationBtnH.constant = self.locationBtnY.constant = 0;
        self.locationBtn.hidden = YES;
    }else{
        // 初始化约束(装箱, 掏箱, 卸箱)
        [self initConstraint];
    }
}

#pragma mark - pickerDateDelegate
// date
-(void)pickerDate:(STPickerDate *)pickerDate year:(NSInteger)year month:(NSInteger)month day:(NSInteger)day {
    // 更新日期
    [self.dateBtn setTitle:[NSString stringWithFormat:@"  %ld-%@-%@", year, [self digitJudge:month], [self digitJudge:day]] forState:UIControlStateNormal];
}
// 数字位数判断
- (NSString *)digitJudge:(NSInteger)number
{
    // 转换的字符串
    NSString *numberStr;
    
    if (number < 10) {
        numberStr = [NSString stringWithFormat:@"0%ld",number];
    }else{
        numberStr = [NSString stringWithFormat:@"%ld",number];
    }
    return numberStr;
}

// content
- (void)pickerSingle:(STPickerSingle *)pickerSingle selectedTitle:(NSString *)selectedTitle
{
    if ([pickerSingle.title isEqualToString:TransportTitle]) {
        [self.transportBtn setTitle:[NSString stringWithFormat:@"  %@", selectedTitle] forState:UIControlStateNormal];
    }else if ([pickerSingle.title isEqualToString:LocationTitle]) {
        [self.locationBtn setTitle:[NSString stringWithFormat:@"  %@", selectedTitle] forState:UIControlStateNormal];
    }else if ([pickerSingle.title isEqualToString:OriginTitle]) {
        [self.originBtn setTitle:[NSString stringWithFormat:@"  %@", selectedTitle] forState:UIControlStateNormal];
    }else if ([pickerSingle.title isEqualToString:TerminalTitle]) {
        [self.terminalBtn setTitle:[NSString stringWithFormat:@"  %@", selectedTitle] forState:UIControlStateNormal];
    }else if ([pickerSingle.title isEqualToString:SupplierTitle]) {
        [self.supplierBtn setTitle:[NSString stringWithFormat:@"  %@", selectedTitle] forState:UIControlStateNormal];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
